﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;

namespace SABI
{
    public partial class frm0001C : Form
    {
        public frm0001C()
        {
            InitializeComponent();
        }

        private void frm0001C_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private string sql = "";

        private void frm0001C_Shown(object sender, EventArgs e)
        {
            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID<>'G00' Order by AccountID";
            TDBCboPurchaseAccountID.DataSource = FRM.GetDataTable(sql);
            TDBCboPurchaseAccountID.SelectedValue = FRM.PurchaseAccountID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G01' Order by AccountID";
            TDBCBCashAccountID.DataSource = FRM.GetDataTable(sql);
            TDBCBCashAccountID.SelectedValue = FRM.CashAccountID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G02' Order by AccountID";
            TDBCBAssetAccountID.DataSource = FRM.GetDataTable(sql);
            TDBCBAssetAccountID.SelectedValue = FRM.AssetChangeValue;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G03' Order by AccountID";
            DataTable dt1  = FRM.GetDataTable(sql);
            TDCBReciedAccountID.DataSource = dt1;
            TDBCboVATInAccountID.DataSource = dt1;
            TDCBReciedAccountID.SelectedValue = FRM.ReceivedAccountID;
            TDBCboVATInAccountID.SelectedValue = FRM.VATInAccountID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G04' Order by AccountID";
            DataTable dt2 = FRM.GetDataTable(sql);
            TDCBPayableAccountID.DataSource = dt2;
            TDBCboVATOutAccountID.DataSource = dt2;
            TDCBPayableAccountID.SelectedValue = FRM.PayableAccountID;
            TDBCboVATOutAccountID.SelectedValue = FRM.VATOutAccountID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G05' Order by AccountID";
            TDBCboStockAccountID.DataSource = FRM.GetDataTable(sql);
            TDBCboStockAccountID.SelectedValue = FRM.StockAccountID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G07' Order by AccountID";
            DataTable dt3 = FRM.GetDataTable(sql);
            TDCBTurnOverAccountID.DataSource = dt3;
            TDBCboInterestExchangeAccID.DataSource = dt3;
            TDCBTurnOverAccountID.SelectedValue = FRM.TurnOverAccountID;
            TDBCboInterestExchangeAccID.SelectedValue = FRM.InterestExchangeAccID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G06' Order by AccountID";
            DataTable dt4 = FRM.GetDataTable(sql);
            TDCBPrimeCostAccountID.DataSource = dt4;
            TDBCboLossExchangeAccID.DataSource = dt4;
            TDBCboCommissionAccountID.DataSource = dt4;
            TDCBPrimeCostAccountID.SelectedValue = FRM.PrimeCostAccountID;
            TDBCboLossExchangeAccID.SelectedValue = FRM.LossExchangeAccID;
            TDBCboCommissionAccountID.SelectedValue = FRM.CommissionAccountID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 and GroupID ='G99' Order by AccountID";
            DataTable dt5 = FRM.GetDataTable(sql);
            TDBCboDifferenceAccountID.DataSource = dt5;
            TDBCboDifferenceAccountID.SelectedValue = FRM.DifferenceAccountID;

            sql = "Select AccountID,AccountName From tb0025 Where IsNotShow = 0 and Disabled =0 Order by AccountID";
            DataTable dt6 = FRM.GetDataTable(sql);
            TDBCboPreCostAccountID.DataSource = dt6;
            TDBCboPreCostAccountID2.DataSource = dt6;
            TDBCboPreCostAccountID.SelectedValue = FRM.PreCostAccountID;
            TDBCboPreCostAccountID2.SelectedValue = FRM.PreCostAccountID2;



        }

        private void TDBCBCashAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCashAccountName.Text = TDBCBCashAccountID.SelectedItem != null ? TDBCBCashAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDCBReciedAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtReceivedAccountName.Text = TDCBReciedAccountID.SelectedItem != null ? TDCBReciedAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDCBPayableAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtPayableAccountName.Text = TDCBPayableAccountID.SelectedItem != null ? TDCBPayableAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDCBTurnOverAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTurnOverAccountName.Text = TDCBTurnOverAccountID.SelectedItem != null ? TDCBTurnOverAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDCBPrimeCostAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtPrimeCostAccountName.Text = TDCBPrimeCostAccountID.SelectedItem != null ? TDCBPrimeCostAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboVATInAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtVATInAccountName.Text = TDBCboVATInAccountID.SelectedItem != null ? TDBCboVATInAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboVATOutAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtVATOutAccountName.Text = TDBCboVATOutAccountID.SelectedItem != null ? TDBCboVATOutAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboDifferenceAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDifferenceAccountID.Text = TDBCboDifferenceAccountID.SelectedItem != null ? TDBCboDifferenceAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboInterestExchangeAccID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInterestExchangeAccID.Text = TDBCboInterestExchangeAccID.SelectedItem != null ? TDBCboInterestExchangeAccID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboLossExchangeAccID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtLossExchangeAccID.Text = TDBCboLossExchangeAccID.SelectedItem != null ? TDBCboLossExchangeAccID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboPreCostAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtPreCostAccountName.Text = TDBCboPreCostAccountID.SelectedItem != null ? TDBCboPreCostAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboPreCostAccountID2_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtPreCostAccountName2.Text = TDBCboPreCostAccountID2.SelectedItem != null ? TDBCboPreCostAccountID2.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboCommissionAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCommissionAccountID.Text = TDBCboCommissionAccountID.SelectedItem != null ? TDBCboCommissionAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCBAssetAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtAssetAccountName.Text = TDBCBAssetAccountID.SelectedItem != null ? TDBCBAssetAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboStockAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtStockAccountID.Text = TDBCboStockAccountID.SelectedItem != null ? TDBCboStockAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboPurchaseAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtPurchaseAccountID.Text = TDBCboPurchaseAccountID.SelectedItem != null ? TDBCboPurchaseAccountID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FRM.CashAccountID = TDBCBCashAccountID.Text;
            FRM.ReceivedAccountID = TDCBReciedAccountID.Text;
            FRM.PayableAccountID = TDCBPayableAccountID.Text;
            FRM.TurnOverAccountID = TDCBTurnOverAccountID.Text;
            FRM.PrimeCostAccountID = TDCBPrimeCostAccountID.Text;
            FRM.StockAccountID = TDBCboStockAccountID.Text;
            FRM.PurchaseAccountID = TDBCboPurchaseAccountID.Text;
            FRM.VATInAccountID = TDBCboVATInAccountID.Text;
            FRM.VATOutAccountID = TDBCboVATOutAccountID.Text;
            FRM.DifferenceAccountID = TDBCboDifferenceAccountID.Text;
            FRM.PreCostAccountID = TDBCboPreCostAccountID.Text;
            FRM.PreCostAccountID2 = TDBCboPreCostAccountID2.Text;
            FRM.InterestExchangeAccID = TDBCboInterestExchangeAccID.Text;
            FRM.LossExchangeAccID = TDBCboLossExchangeAccID.Text;

            sql = string.Format("UPDATE tb0000 SET CashAccountID = '{0}',PreCostAccountID2='{1}',", TDBCBCashAccountID.Text,TDBCboPreCostAccountID2.Text);
            sql += string.Format("ReceivedAccountID='{0}',PayableAccountID='{1}',", TDCBReciedAccountID.Text, TDCBPayableAccountID.Text);
            sql += string.Format("TurnOverAccountID='{0}',PrimeCostAccountID='{1}',", TDCBTurnOverAccountID.Text, TDCBPrimeCostAccountID.Text);
            sql += string.Format("StockAccountID='{0}',PurchaseAccountID='{1}',", TDBCboStockAccountID.Text, TDBCboPurchaseAccountID.Text);
            sql += string.Format("VATInAccountID='{0}',VATOutAccountID='{1}',", TDBCboVATInAccountID.Text, TDBCboVATOutAccountID.Text);
            sql += string.Format("DifferenceAccountID='{0}',PreCostAccountID='{1}',", TDBCboDifferenceAccountID.Text, TDBCboPreCostAccountID.Text);
            sql += string.Format("InterestExchangeAccID='{0}',LossExchangeAccID='{1}'", TDBCboInterestExchangeAccID.Text, TDBCboLossExchangeAccID.Text);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
                SDLL.DLL02.Info("Cập nhật dữ liệu thành công!");
            else
                SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại!");
        }

        
    }
}
